//
//  AgreementView.m
//  DanaKreditLoan
//
//  Created by ZhengChengFeng on 2024/10/18.
//

#import "AgreementView.h"

@implementation AgreementView

- (instancetype)initWithTitle:(NSString *)title withTitleColor:(UIColor *)color protocol:(NSString *)protocol withProtocolColor:(UIColor *)proColor fontSize:(CGFloat)fontSize target:(id)targer withSel:(BOOL)isSel withAgreeImg:(NSString *)agreeImgName withUnagreeImg:(NSString *)unagreeImgName{
    
    self = [super init];
    if (self) {
        self.title = title;
        self.protocol = protocol;
        self.titleColor = color;
        self.protocolColor = proColor;
        self.fontSize = fontSize;
        self.delegate = targer;
        self.isSel = isSel;
        self.agreeImgNameStr = agreeImgName;
        self.unagreeImgNameStr = unagreeImgName;
        [self setupUI];
    }
    return self;
}

- (void)setupUI{
    
    UIImage * agreeImage = [UIImage imageNamed:self.agreeImgNameStr];
    
    UIImage * unagreeImage = [UIImage imageNamed:self.unagreeImgNameStr];
    
    UIButton * agreeButton = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, agreeImage.size.width, agreeImage.size.height)];
    
    [agreeButton setBackgroundImage:agreeImage forState:UIControlStateSelected];
   
    [agreeButton setBackgroundImage:unagreeImage forState:UIControlStateNormal];
   
    [agreeButton addTarget:self action:@selector(clickedAgreeButton:) forControlEvents:UIControlEventTouchUpInside];
    
    if (self.isSel) {
        [self clickedAgreeButton:agreeButton];
    }
    
    
    [self addSubview:agreeButton];
    
    UILabel * titleLabel = [UILabel createLabel:[UIFont fontWithName:kFutura size:12] color:self.titleColor];
    titleLabel.text = self.title;
    [titleLabel sizeToFit];
    titleLabel.left = agreeButton.right + 5;
    [self addSubview:titleLabel];
    
    UIButton * protocolButton = [[UIButton alloc]init];
    protocolButton.titleLabel.font = [UIFont fontWithName:kFutura size:12];
    [protocolButton setTitleColor:self.protocolColor forState:UIControlStateNormal];
    [protocolButton setTitle:self.protocol forState:UIControlStateNormal];
    [protocolButton addTarget:self action:@selector(clickProtocalBtn) forControlEvents:UIControlEventTouchUpInside];
    [protocolButton sizeToFit];
    protocolButton.left = titleLabel.right;
    [self addSubview:protocolButton];
    
    self.width = protocolButton.right;
    self.height = MAX(agreeButton.height, titleLabel.height);
    titleLabel.centerY = protocolButton.centerY = agreeButton.centerY = self.height * 0.5;
        
}

- (void)clickedAgreeButton:(UIButton *)agreeButton{
    
    agreeButton.selected = !agreeButton.selected;
    if (self.delegate && [self.delegate respondsToSelector:@selector(agreeViewWillAgreeProtocol:)]) {
        [self.delegate agreeViewWillAgreeProtocol:agreeButton.selected];
    }
}

-(void)clickProtocalBtn{
    
    if (self.delegate && [self.delegate respondsToSelector:@selector(agreeViewWillShowProtocol)]) {
        [self.delegate agreeViewWillShowProtocol];
    }
}


/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end

